// Created by Windward Studios - no copyright is claimed. This code can be used in
// any manner by anyone for any reason. There is no copyright of any kind on it. You may
// use it in commercial products. You may change it without sharing those changes.
// We ask that you keep the "created by Windward Studios" in a comment at the top.

using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Configuration;
using net.windward.utils.ado.SqlServer;
using NUnit.Framework;

// bugbug - test FK:PK between identically named tables, identically named pairs of columns, but different schemas.

namespace net.windward.utils.ado
{
	[TestFixture]
	public class TestSqlServerProvider
	{
		private string serverName2008;
		private string databaseName2008;
		private string username2008;
		private string password2008;
		private string connStr2008;

		readonly DbProviderFactory providerFactory = DbProviderFactories.GetFactory(WrSqlServerVendor.FactoryClass);

		[SetUp]
		public void Init()
		{
			NameValueCollection appSettings = HttpContext.Current == null ? ConfigurationManager.AppSettings : WebConfigurationManager.AppSettings;

			serverName2008 = appSettings["SqlServerServer"];
			databaseName2008 = appSettings["SqlServerDatabase"];
			username2008 = appSettings["SqlServerUsername"];
			password2008 = appSettings["SqlServerPassword"];

			ConnectionStringSettingsCollection coll = HttpContext.Current == null ? ConfigurationManager.ConnectionStrings : WebConfigurationManager.ConnectionStrings;
			connStr2008 = coll["SqlServerRead"].ConnectionString;
		}

		[Test]
		public void TestCreateProvider()
		{

			WrSqlServerVendor vendor = (WrSqlServerVendor)WrProviderFactories.CreateProvider("the Sql Server name", null, WrSqlServerVendor.FactoryClass);
			Assert.IsNotNull(vendor);
			Assert.AreEqual(true, vendor.CanEnumerateDatabases);
			Assert.AreEqual(true, vendor.CanEnumerateServers);
			//bugbug			Assert.AreEqual(true, vendor.CanLaunchAdministrator);
			Assert.AreEqual(true, vendor.CanRunScripts);
			Assert.AreEqual(true, vendor.CanUseTrustedConnection);
			Assert.AreEqual(WrVendor.DATABASE_MODE.REQUIRED, vendor.DatabaseMode);
			Assert.AreEqual(false, vendor.HasProviders);
			Assert.AreEqual("the Sql Server name", vendor.Name);
			Assert.AreEqual("Data Source=abc;Initial Catalog=def;User ID=123;Password=456",
				vendor.ConnectionString("abc", "def", new WrCredentials("123", "456"), true));
			Assert.AreEqual("Data Source=abc;Initial Catalog=def;Integrated Security=True",
				vendor.ConnectionString("abc", "def", new WrCredentials(), true));

			// enum servers - make sure has test one
			IWrServer[] servers = vendor.GetServers();
			Assert.IsTrue(servers.Length > 0);
			bool gotServer = false;
			string lcServerName = GetJustRoot(serverName2008);
			foreach (IWrServer server in servers)
			{
				Assert.IsTrue(server is WrSqlServerServer);
				string str = GetJustRoot(server.Name);
				if (str == lcServerName)
					gotServer = true;
			}
			Assert.IsTrue(gotServer);
		}

		private static string GetJustRoot(string serverName)
		{
			string lcServerName = serverName.ToLower();
			if (lcServerName.IndexOf('\\') != -1)
				lcServerName = lcServerName.Substring(0, lcServerName.IndexOf('\\'));
			if (lcServerName.IndexOf('/') != -1)
				lcServerName = lcServerName.Substring(0, lcServerName.IndexOf('/'));
			return lcServerName.Trim();
		}

		[Test]
		public void TestNorthwind2000()
		{
			NameValueCollection appSettings = HttpContext.Current == null ? ConfigurationManager.AppSettings : WebConfigurationManager.AppSettings;
			string serverName = appSettings["SqlServerServer2000"];
			string username = appSettings["SqlServerUsername2000"];
			string password = appSettings["SqlServerPassword2000"];

			NorthwindTest(serverName, appSettings["SqlServerNorthwind2000"], username, password);
			KailuaTest(serverName, appSettings["SqlServerTestDb2000"], username, password, 1);
		}

		[Test]
		public void TestNorthwind2005()
		{
			NameValueCollection appSettings = HttpContext.Current == null ? ConfigurationManager.AppSettings : WebConfigurationManager.AppSettings;
			string serverName = appSettings["SqlServerServer2005"];
			string username = appSettings["SqlServerUsername2005"];
			string password = appSettings["SqlServerPassword2005"];

			AdventureworksTest(serverName, appSettings["SqlServerAdventureWorks2005"], username, password);
			NorthwindTest(serverName, appSettings["SqlServerNorthwind2005"], username, password);
			KailuaTest(serverName, appSettings["SqlServerTestDb2005"], username, password, 2);
		}

		[Test]
		public void TestNorthwind2008()
		{
			NameValueCollection appSettings = HttpContext.Current == null ? ConfigurationManager.AppSettings : WebConfigurationManager.AppSettings;
			string serverName = appSettings["SqlServerServer2008"];
			string username = appSettings["SqlServerUsername2008"];
			string password = appSettings["SqlServerPassword2008"];

			AdventureworksTest(serverName, appSettings["SqlServerAdventureWorks2008"], username, password);
			NorthwindTest(serverName, appSettings["SqlServerNorthwind2008"], username, password);
			KailuaTest(serverName, appSettings["SqlServerTestDb2008"], username, password, 2);
		}

		public void NorthwindTest(string serverName, string databaseName, string username, string password)
		{

			// enum databases, make sure has Northwind
			WrSqlServerServer server = new WrSqlServerServer(providerFactory, serverName);
			Assert.AreEqual(serverName, server.Name);
			IWrDatabase[] databases = server.GetDatabases(new WrCredentials(username, password));
			Assert.IsTrue(databases.Length > 0);
			WrSqlServerDatabase db = null;
			foreach (IWrDatabase dbOn in databases)
			{
				Assert.IsTrue(dbOn is WrSqlServerDatabase);
				if (dbOn.Name == databaseName)
					db = (WrSqlServerDatabase) dbOn;
			}
			Assert.IsNotNull(db);

			// Northwind tables
			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.USER | WrVendor.ELEM_OWNER.META_DATA, null);
			WrTable[] tables = db.Tables;
			Assert.AreEqual(13, tables.Length);
			Assert.AreEqual("dbo.Categories", tables[0].FullName);
			Assert.IsFalse(tables[0].IsSystem);
			Assert.AreEqual("dbo.Territories", tables[12].FullName);
			Assert.IsFalse(tables[12].IsSystem);

			// Northwind views
			WrView[] views = db.Views;
			Assert.AreEqual(16, views.Length);
			Assert.AreEqual("dbo.Alphabetical list of products", views[0].FullName);
			Assert.IsFalse(views[0].IsSystem);
			Assert.AreEqual("dbo.Summary of Sales by Year", views[15].FullName);
			Assert.IsFalse(views[15].IsSystem);

			// Northwind stored procedures
			WrStoredProcedure[] procs = db.StoredProcedures;
			Assert.AreEqual(7, procs.Length);
			Assert.AreEqual("CustOrderHist", procs[0].FullName);
			Assert.IsFalse(procs[0].IsSystem);
			Assert.AreEqual("Ten Most Expensive Products", procs[6].FullName);
			Assert.IsFalse(procs[6].IsSystem);

			// params - Northwind CustOrderHist
			WrProcParam[] wpp = db.StoredProcedures[0].Parameters;
			Assert.AreEqual(1, wpp.Length);
			Assert.AreEqual(5, wpp[0].DataLength);
			Assert.AreEqual(DbType.StringFixedLength, wpp[0].DbType);
			Assert.AreEqual(ParameterDirection.Input, wpp[0].Direction);
			Assert.AreEqual(false, wpp[0].HasDefault);
			Assert.AreEqual("@CustomerID", wpp[0].Name);

			// columns - Northwind Orders
			WrColumn[] cols = db.Tables[7].Columns;
			Assert.AreEqual(14, cols.Length);
			Assert.AreEqual("OrderID", cols[0].FullName);
			Assert.AreEqual(DbType.Int32, cols[0].DbType);
			Assert.AreEqual(null, cols[0].PrimaryKey);

			Assert.AreEqual("CustomerID", cols[1].FullName);
			Assert.AreEqual(DbType.StringFixedLength, cols[1].DbType);
			Assert.AreEqual("dbo.Customers.CustomerID", cols[1].PrimaryKey);

			Assert.AreEqual("EmployeeID", cols[2].FullName);
			Assert.AreEqual(DbType.Int32, cols[2].DbType);
			Assert.AreEqual("dbo.Employees.EmployeeID", cols[2].PrimaryKey);

			Assert.AreEqual("ShipCountry", cols[13].FullName);
			Assert.AreEqual(DbType.String, cols[13].DbType);

			// no metadata - columns - Northwind Orders
			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.USER, null);
			cols = db.Tables[7].Columns;
			Assert.AreEqual(14, cols.Length);
			Assert.AreEqual("CustomerID", cols[1].FullName);
			Assert.AreEqual(DbType.StringFixedLength, cols[1].DbType);
			Assert.AreEqual(null, cols[1].PrimaryKey);
		}

		public void KailuaTest(string serverName, string databaseName, string username, string password, int numTables)
		{

			// KailuaTest - all types
			WrSqlServerDatabase db = new WrSqlServerDatabase(providerFactory, serverName, databaseName);
			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.USER | WrVendor.ELEM_OWNER.META_DATA, null);

			WrTable [] tables = db.Tables;
			Assert.AreEqual(numTables, tables.Length);
			Assert.AreEqual("dbo.AllTypes", tables[0].FullName);
			Assert.AreEqual("A table with all types", tables[0].Description);
			Assert.IsFalse(tables[0].IsSystem);

			// columns AllTypes
			WrColumn [] cols = db.Tables[0].Columns;
			Assert.AreEqual(24, cols.Length);

			Assert.AreEqual("pk", cols[0].FullName);
			Assert.AreEqual("the primary key", cols[0].Description);
			Assert.AreEqual(DbType.Int64, cols[0].DbType);
			Assert.AreEqual(null, cols[0].PrimaryKey);

			Assert.AreEqual("kBinary", cols[1].FullName);
			Assert.AreEqual(null, cols[1].Description);
			Assert.AreEqual(DbType.Binary, cols[1].DbType);
			Assert.AreEqual(null, cols[1].PrimaryKey);

			// bugbug - assert rest of columns

			Assert.AreEqual("kInt", cols[8].FullName);
			Assert.AreEqual(null, cols[8].Description);
			Assert.AreEqual(DbType.Int32, cols[8].DbType);
			Assert.AreEqual(null, cols[8].PrimaryKey);

			Assert.AreEqual("kText", cols[17].FullName);
			Assert.AreEqual("the dbo table", cols[17].Description);
			Assert.AreEqual(DbType.AnsiString, cols[17].DbType);
			Assert.AreEqual(null, cols[17].PrimaryKey);

			Assert.AreEqual("kVarChar", cols[22].FullName);
			Assert.AreEqual(null, cols[22].Description);
			Assert.AreEqual(DbType.AnsiString, cols[22].DbType);
			Assert.AreEqual(null, cols[22].PrimaryKey);

			Assert.AreEqual("xxx", cols[23].FullName);
			Assert.AreEqual(null, cols[23].Description);
			Assert.AreEqual(DbType.Decimal, cols[23].DbType);
			Assert.AreEqual(null, cols[23].PrimaryKey);

			// Sql Server 2005, 2008
			if (numTables > 1 )
			{
				Assert.AreEqual("MySchema.AllTypes", tables[1].FullName);
				Assert.AreEqual("the MySchema table", tables[1].Description);
				Assert.IsFalse(tables[1].IsSystem);

				// columns 
				cols = db.Tables[1].Columns;
				Assert.AreEqual(3, cols.Length);

				Assert.AreEqual("MyPk", cols[0].FullName);
				Assert.AreEqual(null, cols[0].Description);
				Assert.AreEqual(DbType.Int32, cols[0].DbType);
				Assert.AreEqual(null, cols[0].PrimaryKey);

				Assert.AreEqual("kText", cols[1].FullName);
				Assert.AreEqual("this is in the MySchema table", cols[1].Description);
				Assert.AreEqual(DbType.AnsiString, cols[1].DbType);
				Assert.AreEqual(null, cols[1].PrimaryKey);

				Assert.AreEqual("MyFk", cols[2].FullName);
				Assert.AreEqual(null, cols[2].Description);
				Assert.AreEqual(DbType.Int64, cols[2].DbType);
				Assert.AreEqual(null, cols[2].PrimaryKey);
			}
		}

		public void AdventureworksTest(string serverName, string databaseName, string username, string password)
		{

			WrSqlServerDatabase db = new WrSqlServerDatabase(providerFactory, serverName, databaseName);
			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.USER | WrVendor.ELEM_OWNER.META_DATA, null);

			// AdventureWorks tables
			WrTable[] tables = db.Tables;
			Assert.AreEqual(70, tables.Length);
			Assert.AreEqual("dbo.AWBuildVersion", tables[0].FullName);
			Assert.IsFalse(tables[0].IsSystem);
			Assert.AreEqual("HumanResources.Department", tables[3].FullName);
			Assert.IsFalse(tables[3].IsSystem);
			Assert.AreEqual("Sales.StoreContact", tables[69].FullName);
			Assert.IsFalse(tables[69].IsSystem);

			// AdventureWorks views
			WrView[] views = db.Views;
			Assert.AreEqual(17, views.Length);
			Assert.AreEqual("HumanResources.vEmployee", views[0].FullName);
			Assert.IsFalse(views[0].IsSystem);
			Assert.AreEqual("Sales.vStoreWithDemographics", views[16].FullName);
			Assert.IsFalse(views[16].IsSystem);

			// AdventureWorks stored procedures
			WrStoredProcedure[] procs = db.StoredProcedures;
			Assert.AreEqual(9, procs.Length);
			Assert.AreEqual("uspGetBillOfMaterials", procs[0].FullName);
			Assert.IsFalse(procs[0].IsSystem);
			Assert.AreEqual("HumanResources.uspUpdateEmployeePersonalInfo", procs[8].FullName);
			Assert.IsFalse(procs[8].IsSystem);

			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.SYSTEM | WrVendor.ELEM_OWNER.META_DATA, null);
			procs = db.StoredProcedures;
			Assert.AreEqual(0, procs.Length);
			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.SYSTEM | WrVendor.ELEM_OWNER.USER | WrVendor.ELEM_OWNER.META_DATA, null);
			procs = db.StoredProcedures;
			Assert.AreEqual(9, procs.Length);

			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.USER | WrVendor.ELEM_OWNER.META_DATA, null);

			// AdventureWorks HumanResources.Employee columns
			WrColumn[] cols = db.Tables[4].Columns;
			Assert.AreEqual(16, cols.Length);
			Assert.AreEqual("EmployeeID", cols[0].FullName);
			Assert.AreEqual(DbType.Int32, cols[0].DbType);
			Assert.AreEqual(null, cols[0].PrimaryKey);

			Assert.AreEqual("ContactID", cols[2].FullName);
			Assert.AreEqual(DbType.Int32, cols[2].DbType);
			Assert.AreEqual("Person.Contact.ContactID", cols[2].PrimaryKey);

			Assert.AreEqual("ManagerID", cols[4].FullName);
			Assert.AreEqual(DbType.Int32, cols[4].DbType);
			Assert.AreEqual("HumanResources.Employee.EmployeeID", cols[4].PrimaryKey);

			Assert.AreEqual("ModifiedDate", cols[15].FullName);
			Assert.AreEqual(DbType.DateTime, cols[15].DbType);
			Assert.AreEqual(null, cols[15].PrimaryKey);

			// no meta
			db.LoadMetadata(new WrCredentials(username, password), WrVendor.ELEM_OWNER.USER, null);

			// AdventureWorks HumanResources.Employee columns
			cols = db.Tables[4].Columns;
			Assert.AreEqual(16, cols.Length);

			Assert.AreEqual("ContactID", cols[2].FullName);
			Assert.AreEqual(DbType.Int32, cols[2].DbType);
			Assert.AreEqual(null, cols[2].PrimaryKey);
		}

		[Test]
		public void TestNorthwindEnumerationConnStr()
		{

			// enum databases, make sure has test one
			WrSqlServerServer server = new WrSqlServerServer(providerFactory, serverName2008);
			Assert.AreEqual(serverName2008, server.Name);
			IWrDatabase[] databases = server.GetDatabases(connStr2008);
			Assert.IsTrue(databases.Length > 0);
			WrSqlServerDatabase db = null;
			foreach (IWrDatabase dbOn in databases)
			{
				Assert.IsTrue(dbOn is WrSqlServerDatabase);
				if (dbOn.Name == databaseName2008)
					db = (WrSqlServerDatabase)dbOn;
			}
			Assert.IsNotNull(db);

			db.LoadMetadata(connStr2008, WrVendor.ELEM_OWNER.USER, null);

			// Northwind tables
			WrTable[] tables = db.Tables;
			Assert.AreEqual(13, tables.Length);
			Assert.AreEqual("dbo.Categories", tables[0].FullName);
			Assert.IsFalse(tables[0].IsSystem);
			Assert.AreEqual("dbo.Territories", tables[12].FullName);
			Assert.IsFalse(tables[12].IsSystem);

			// Northwind views
			WrView[] views = db.Views;
			Assert.AreEqual(16, views.Length);
			Assert.AreEqual("dbo.Alphabetical list of products", views[0].FullName);

			// Northwind stored procedures
			WrStoredProcedure[] procs = db.StoredProcedures;
			Assert.AreEqual(7, procs.Length);
			Assert.AreEqual("CustOrderHist", procs[0].FullName);
			Assert.IsFalse(procs[0].IsSystem);
			Assert.AreEqual("Ten Most Expensive Products", procs[6].FullName);
			Assert.IsFalse(procs[6].IsSystem);

			// Northwind.Employees columns
			WrColumn[] cols = db.Tables[4].Columns;
			Assert.AreEqual(18, cols.Length);
			Assert.AreEqual("EmployeeID", cols[0].FullName);
			Assert.AreEqual(DbType.Int32, cols[0].DbType);
			Assert.AreEqual("PhotoPath", cols[17].FullName);
			Assert.AreEqual(DbType.String, cols[17].DbType);
		}

		[Test]
		public void TestTrusted()
		{

			WrSqlServerServer server = new WrSqlServerServer(providerFactory, serverName2008);
			IWrDatabase[] dbUsername = server.GetDatabases(new WrCredentials(username2008, password2008));
			IWrDatabase[] dbTrusted = server.GetDatabases(new WrCredentials());
			Assert.AreEqual(dbUsername.Length, dbTrusted.Length);
			for (int ind = 0; ind < dbUsername.Length; ind++)
				Assert.AreEqual(dbUsername[ind].Name, dbTrusted[ind].Name);

			dbUsername[0].LoadMetadata(connStr2008, WrVendor.ELEM_OWNER.USER, null);
			dbTrusted[0].LoadMetadata(connStr2008, WrVendor.ELEM_OWNER.USER, null);

			WrTable[] tblUsername = dbUsername[0].Tables;
			WrTable[] tblTrusted = dbTrusted[0].Tables;
			Assert.AreEqual(tblUsername.Length, tblTrusted.Length);
			for (int ind = 0; ind < tblUsername.Length; ind++)
				Assert.AreEqual(tblUsername[ind].FullName, tblTrusted[ind].FullName);
		}

		// note - cannot insert a timestamp
		private readonly string[] colNames = {"kBinary", "kBoolean", "kChar", "kDateTime", "kDecimal", "kFloat", "kImage", "kInt", 
				"kMoney","kNchar", "kNtext", "kNvarchar", "kReal", "kSmallDateTime", "kSmallInt", "kSmallMoney", "kText",
				"kTinyInt", "kUniqueIdentifier", "kVarBinary", "kVarChar"};
		private readonly object[] colValues = {new byte[10], true, "dave", DateTime.Now, new Decimal(1.23), 4.56, new byte [5], 789,
				12.34, "shirley", "winter", "lucky", 56.78, DateTime.Now, 90, 1.23, "zoe",
				5, new Guid(), new byte [22], "cody"};

		[Test]
		public void TestInsert()
		{

			using (WrConnection conn = new WrConnection("SqlServerWrite"))
			{
				conn.Open();

				// empty it to start the same
				using (WrCommand cmd = conn.CreateWrCommand())
				{
					cmd.CommandText = "delete from AllTypes";
					cmd.ExecuteNonQuery();
				}

				// test get the PK back
				long[] pk = new long[2];
				using (WrCommand cmd = conn.CreateWrCommand())
				{
					cmd.CommandText = "insert into AllTypes (kNvarchar) values (?)";
					cmd.Set(0, "dave");
					pk[0] = cmd.ExecuteInsert("pk");
					Assert.IsTrue(pk[0] > 0);
				}

				using (WrCommand cmd = conn.CreateWrCommand())
				{
					cmd.CommandText = "insert into AllTypes (kNvarchar) values (?)";
					cmd.Set(0, "shirley");
					pk[1] = cmd.ExecuteInsert("pk");
					Assert.IsTrue(pk[1] > pk[0]);
				}

				// test insert each var type
				for (int ind = 0; ind < colNames.Length; ind++)
				{
					using (WrCommand cmd = conn.CreateWrCommand())
					{
						cmd.CommandText = "insert into AllTypes (" + colNames[ind] + ") values (?)";
						cmd.Set(0, colValues[ind]);
						cmd.ExecuteInsert("pk");
					}
				}

				// write all together
				using (WrCommand cmd = conn.CreateWrCommand())
				{
					StringBuilder command = new StringBuilder("insert into AllTypes (");
					for (int ind = 0; ind < colNames.Length; ind++)
						command.Append(colNames[ind] + ",");
					command.Remove(command.Length - 1, 1);
					command.Append(") values (");
					for (int ind = 0; ind < colNames.Length; ind++)
						command.Append("?,");
					command.Remove(command.Length - 1, 1);
					command.Append(")");
					cmd.CommandText = command.ToString();

					for (int ind = 0; ind < colNames.Length; ind++)
						cmd.Set(ind, colValues[ind]);

					pk[0] = cmd.ExecuteInsert("pk");
				}

				// test read each var type
				byte[] data = new byte[20];
				char[] chrs = new char[20];
				using (WrCommand cmd = conn.CreateWrCommand())
				{
					cmd.CommandText = "select * from AllTypes where pk = ?";
					cmd.Set(0, pk[0]);
					using (WrDataReader reader = cmd.WrExecuteReader())
					{
						Assert.IsTrue(reader.Read());
						Assert.AreEqual(pk[0], reader.GetInt64(0));
						Assert.AreEqual(4, reader.GetBytes(1, 0, data, 0, 4));
						Assert.AreEqual(colValues[1], reader.GetBoolean(2));
						Assert.AreEqual("dave      ", reader.GetString(3));
						Assert.AreEqual(((DateTime)colValues[3]).Date, reader.GetDateTime(4).Date);
						Assert.AreEqual(colValues[4], reader.GetDecimal(5));
						Assert.AreEqual(colValues[5], reader.GetValue(6));
						Assert.AreEqual(colValues[5], reader.GetDouble(6));
						Assert.AreEqual(4, reader.GetBytes(7, 0, data, 0, 4));
						Assert.AreEqual(colValues[7], reader.GetInt32(8));
						Assert.AreEqual(new decimal(12.3400), reader.GetDecimal(9));
						Assert.AreEqual(new decimal(12.3400), reader.GetDecimal(9));
						Assert.AreEqual("shirley   ", reader.GetString(10));
						Assert.AreEqual(colValues[10], reader.GetString(11));
						Assert.AreEqual(colValues[11], reader.GetString(12));
						Assert.IsTrue(Math.Abs((double)colValues[12] - reader.GetFloat(13)) < 0.001);
						Assert.AreEqual(((DateTime)colValues[13]).Date, reader.GetDateTime(14).Date);
						Assert.AreEqual(colValues[14], reader.GetInt16(15));
						Assert.AreEqual(new decimal(1.2300), reader.GetDecimal(16));
						Assert.AreEqual(colValues[16], reader.GetString(17));
						Assert.IsTrue(reader.GetTimestamp(18) > 0);
						Assert.AreEqual(colValues[17], reader.GetByte(19));
						Assert.AreEqual(colValues[18], reader.GetGuid(20));
						Assert.AreEqual(4, reader.GetBytes(21, 0, data, 0, 4));
						Assert.AreEqual(4, reader.GetChars(22, 0, chrs, 0, 4));

						// remaining calls
						Assert.IsFalse(reader.IsDBNull(1));
						Assert.AreEqual("kBoolean", reader.GetName(2));
						Assert.AreEqual("char", reader.GetDataTypeName(3));
						Assert.AreEqual(typeof(DateTime), reader.GetFieldType(4));
						Assert.AreEqual(5, reader.GetOrdinal("kDecimal"));
						Assert.AreEqual(24, reader.FieldCount);
						Assert.AreEqual("lucky", reader[12]);
						Assert.AreEqual("lucky", reader["kNvarchar"]);

						object[] all = new object[24];
						Assert.AreEqual(24, reader.GetValues(all));
						Assert.AreEqual(pk[0], all[0]);
						Assert.AreEqual("lucky", all[12]);
					}
				}

				// test select top N
				using (WrCommand cmd = conn.CreateWrCommand())
				{
					cmd.CommandText = "  select   * from AllTypes order by pk DESC  top  1  ";
					Assert.AreEqual("select top  1 * from AllTypes order by pk DESC", cmd.CommandText);
					using (WrDataReader reader = cmd.WrExecuteReader())
					{
						Assert.IsTrue(reader.Read());
						Assert.AreEqual(pk[0], reader.GetInt64(0));
					}
				}
			}
		}

		//		[Test]
		public void TestLaunch()
		{

			WrSqlServerVendor provider = (WrSqlServerVendor)WrProviderFactories.CreateProvider("My Sql Server name", null, "System.Data.SqlClient");
			Assert.IsTrue(provider.RunAdministrator());
		}

		private const string script = "..\\..\\DDL_Scripts\\SqlServer.ddl";

		[Test]
		public void TestScript()
		{
			WrSqlServerVendor provider = (WrSqlServerVendor)WrProviderFactories.CreateProvider("My Sql Server name", null, "System.Data.SqlClient");
			databaseName2008 = "KailuaTest";
			Assert.IsTrue(provider.RunScript(serverName2008, databaseName2008,
				new StreamReader(Path.GetFullPath(script)), new WrCredentials(username2008, password2008)));
		}
	}
}
